<template>
  <div class="add-admin">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="adminName">
        <el-input v-model="ruleForm.adminName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="adminPassword">
        <el-input
          type="password"
          v-model="ruleForm.adminPassword"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确认修改</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="handleBack()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "admin-edit",
  data() {
    var checkadminName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.adminPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        _id: "",
        adminPassword: "",
        checkPass: "",
        adminName: "",
      },
      rules: {
        adminPassword: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        adminName: [{ validator: checkadminName, trigger: "blur" }],
      },
    };
  },
  mounted() {
    const obj = JSON.parse(this.$route.params.obj);
    this.ruleForm._id = obj._id;
    this.ruleForm.adminName = obj.adminName;
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { adminName, adminPassword, _id } = this.ruleForm;
          this.$store
            .dispatch(`admin/update`, {
              _id,
              adminName,
              adminPassword,
            })
            .then((res) => {
              console.log(res);
              if (res) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.$router.push("/admin");
              } else {
                this.$message({
                  message: "修改失败",
                  type: "error",
                });
              }
              this.ruleForm.adminPassword = this.ruleForm.adminName = this.ruleForm.checkPass =
                "";
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleBack() {
      this.$router.back();
    },
  },
};
</script>

<style lang="scss" scoped>
.add-admin {
  width: 50%;
  margin: 6% auto;
}
</style>